package com.ruoyi.system.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.BatchExamQuestionDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MineExamQuestion;
import com.ruoyi.system.service.IMineExamQuestionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 试卷题目关联Controller
 * 
 * @author ruoyi
 * @date 2025-01-15
 */
@RestController
@RequestMapping("/system/exam_question")
public class MineExamQuestionController extends BaseController
{
    @Autowired
    private IMineExamQuestionService mineExamQuestionService;

    /**
     * 查询试卷题目关联列表
     */
    @PreAuthorize("@ss.hasPermi('system:exam_question:list')")
    @GetMapping("/list")
    public TableDataInfo list(MineExamQuestion mineExamQuestion)
    {
        startPage();
        List<MineExamQuestion> list = mineExamQuestionService.selectMineExamQuestionList(mineExamQuestion);
        return getDataTable(list);
    }

    /**
     * 导出试卷题目关联列表
     */
    @PreAuthorize("@ss.hasPermi('system:exam_question:export')")
    @Log(title = "试卷题目关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MineExamQuestion mineExamQuestion)
    {
        List<MineExamQuestion> list = mineExamQuestionService.selectMineExamQuestionList(mineExamQuestion);
        ExcelUtil<MineExamQuestion> util = new ExcelUtil<MineExamQuestion>(MineExamQuestion.class);
        util.exportExcel(response, list, "试卷题目关联数据");
    }

    /**
     * 获取试卷题目关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:exam_question:query')")
    @GetMapping(value = "/{examId}")
    public AjaxResult getInfo(@PathVariable("examId") Long examId)
    {
        return success(mineExamQuestionService.selectMineExamQuestionByExamId(examId));
    }

    /**
     * 新增试卷题目关联
     */
    @PreAuthorize("@ss.hasPermi('system:exam_question:add')")
    @Log(title = "试卷题目关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MineExamQuestion mineExamQuestion)
    {
        return toAjax(mineExamQuestionService.insertMineExamQuestion(mineExamQuestion));
    }

    @PreAuthorize("@ss.hasPermi('system:exam_question:add')")
    @Log(title = "试卷题目关联", businessType = BusinessType.INSERT)
    @PostMapping("/addbatch")
    public AjaxResult addBatch(@RequestBody BatchExamQuestionDTO dto) {
        // 参数校验
        if (dto.getExamId() == null) {
            return error("试卷ID不能为空");
        }
        if (dto.getQuestionIds() == null || dto.getQuestionIds().isEmpty()) {
            return error("题目ID列表不能为空");
        }

        // 删除旧数据
        mineExamQuestionService.deleteMineExamQuestionByExamId(dto.getExamId());

        // 构建关联列表
        List<MineExamQuestion> list = dto.getQuestionIds().stream()
                .map(questionId -> {
                    MineExamQuestion eq = new MineExamQuestion();
                    eq.setExamId(dto.getExamId());
                    eq.setQuestionId(questionId);
                    return eq;
                })
                .collect(Collectors.toList());

        // 批量插入
        return toAjax(mineExamQuestionService.insertBatchMineExamQuestion(list));
    }
    /**
     * 修改试卷题目关联
     */
    @PreAuthorize("@ss.hasPermi('system:exam_question:edit')")
    @Log(title = "试卷题目关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MineExamQuestion mineExamQuestion)
    {
        return toAjax(mineExamQuestionService.updateMineExamQuestion(mineExamQuestion));
    }

    /**
     * 删除试卷题目关联
     */
    @PreAuthorize("@ss.hasPermi('system:exam_question:remove')")
    @Log(title = "试卷题目关联", businessType = BusinessType.DELETE)
	@DeleteMapping("/{examIds}")
    public AjaxResult remove(@PathVariable Long[] examIds)
    {
        return toAjax(mineExamQuestionService.deleteMineExamQuestionByExamIds(examIds));
    }

    /** 根据试卷ID查询题目详情 */
    @PreAuthorize("@ss.hasPermi('system:exam_question:query')")
    @GetMapping("/questions/{examId}")
    public AjaxResult getQuestionsByExamId(@PathVariable Long examId) {
        return success(mineExamQuestionService.selectMineQuestionsByExamId(examId));
    }


}
